package com.woniu.command.handler;

import com.woniu.adapter.ProgressAdapter;
import com.woniu.adapter.ProcessHandoverAdapter;
import com.woniu.adapter.WarehousingAdapter;
import com.woniu.exception.LastProcessException;
import com.woniu.exception.NotLastProcessException;
import com.woniu.inlet.vo.WorkingProcedureVo;
import com.woniu.outlet.client.OrderClient;
import com.woniu.outlet.client.PlanClient;
import com.woniu.outlet.client.WorkingProcedures;
import com.woniu.outlet.po.ProPlanPo;
import com.woniu.outlet.po.ProcessHandover;
import com.woniu.outlet.po.Progress;
import com.woniu.outlet.po.WarehousingApplication;
import com.woniu.util.ResponseResult;
import com.woniu.util.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
public class HandCommandHandler {
    @Autowired
    private ProcessHandoverAdapter processHandoverAdapter;
    @Autowired
    private ProgressAdapter progressAdapter;
    @Autowired
    private WarehousingAdapter warehousingAdapter;
    @Autowired
    private OrderClient orderClient;
    @Autowired
    private PlanClient planClient;

    public void addProcessHandover(ProcessHandover processHandover){
//        String body = workingProcedures.getWorkingProcedureByCode(processHandover.getProcessCode()).getBody();
        Long body = orderClient.findIdByOrderNo(processHandover.getOrderCode()).getData();
        processHandoverAdapter.addProcessHandover(processHandover);
        ResponseResult<ProPlanPo> allProPlan = planClient.findAllProPlan(body);
        Long id = allProPlan.getData().getId();
        Progress progress = new Progress();
        progress.setProPlanId(id);
        progress.setProcessId(processHandover.getId().longValue());
        progress.setGoodProNum(processHandover.getGoodNum());
        progress.setScrapNum(processHandover.getCostNum());
        progress.setRepairNum(0);
        progress.setUserId(processHandover.getHandoverPersonId());
        progress.setPassiveUserId(processHandover.getBehandoverPersonId());
        progress.setHandoverTime(new Date());
        progress.setUserId(processHandover.getHandoverPersonId());
        progress.setSpare1(processHandover.getWasteNum()+"");
        addProgress(progress);

        //成品入库
//        if(!body.equals("last")){
//            throw new NotLastProcessException("不是最后一道工序,不是成品,不用入库");
//        }else{
            WarehousingApplication warehousingApplication = new WarehousingApplication();
            warehousingApplication.setWarehousingCode("Ware_"+ SnowflakeIdGenerator.nextId());
            warehousingApplication.setUserId(processHandover.getHandoverPersonId());
            warehousingApplication.setWarehousingTime(new Date());
            warehousingApplication.setPartsCode(processHandover.getPartsCode());
            warehousingApplication.setPartsName(processHandover.getPartsName());
            warehousingApplication.setNum(processHandover.getGoodNum());
            warehousingApplication.setStatus("未入库");
            warehousingApplication.setType("P");
            addWarehousing(warehousingApplication);

//        }
    }

    public void updateProcessHandover(ProcessHandover processHandover){
        processHandoverAdapter.updateProcessHandover(processHandover);
    }

    public void delProcessHandover(Integer id){
        processHandoverAdapter.delProcessHandover(id);
    }

    public void urgent(ProcessHandover processHandover){
        processHandoverAdapter.addProcessHandover(processHandover);
        Long body = orderClient.findIdByOrderNo(processHandover.getOrderCode()).getData();
        ResponseResult<ProPlanPo> allProPlan = planClient.findAllProPlan(body);
        Long id = allProPlan.getData().getId();
        Progress progress = new Progress();
        progress.setProPlanId(id);
        progress.setProcessId(processHandover.getId().longValue());
        progress.setGoodProNum(processHandover.getGoodNum());
        progress.setScrapNum(processHandover.getCostNum());
        progress.setRepairNum(processHandover.getWasteNum());
        progress.setUserId(processHandover.getHandoverPersonId());
        progress.setPassiveUserId(processHandover.getBehandoverPersonId());
        progress.setHandoverTime(new Date());
        progress.setSpare1(processHandover.getWasteNum()+"");
        progress.setUserId(processHandover.getHandoverPersonId());
        addProgress(progress);

//        ResponseEntity<String> workingProcedureVoResponseEntity = workingProcedures.getWorkingProcedureByCode(processHandover.getProcessCode());
//        String sort = workingProcedureVoResponseEntity.getBody();
//        if(sort.equals("last")){
//            throw new LastProcessException("当前已是最后一道工序,请直接将成品入库");
//        }
        processHandoverAdapter.urgent(processHandover);
    };

    public void addProgress(Progress progress){
        progressAdapter.addProgress(progress);
    }

    public void addWarehousing (WarehousingApplication warehousingApplication) {
        warehousingAdapter.addWarehousing(warehousingApplication);
    }
}
